/*
 * Copyright (c) 2017 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.helper;

import com.google.common.collect.Lists;
import com.jnzc.ocr.common.utils.ArrayHandleUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zhangcm
 * @version 1.0, 2017/10/7
 */
public class ShopOrderHelper {

    private ShopOrderHelper(){}

    public static String createNo(Long shopId){
        Date now = DateUtils.getNow();
        StringBuilder result = new StringBuilder();
        // 2位年份
        Integer year = DateUtils.getYear(now);
        result.append(StringUtils.substring(year.toString(), 2, 4));
        // 11位当年时间戳
        Long yearTime = now.getTime() - DateUtils.getYearTime(year);
        result.append(StringUtils.leftPad(yearTime.toString(), 11, "0"));
        // 3位随机值
        result.append(RandomStringUtils.randomNumeric(4));
        // 2位店铺ID
        String shopIdStr = StringUtils.leftPad(shopId.toString(), 3, "0");
        result.append(StringUtils.substring(shopIdStr, shopIdStr.length() - 3, shopIdStr.length()));
        return result.toString();
    }

    public static void main(String[] args) {
        String no = ShopOrderHelper.createNo(123456L);
        System.out.println(no);
        System.out.println(no.length());

        List<Thread> threadList = Lists.newArrayList();
        List<String> list = Lists.newCopyOnWriteArrayList();
        for (int i = 0; i < 1000; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int j = 0; j < 100; j++) {
                        list.add(ShopOrderHelper.createNo(1L));
                        // list.add(UUID.randomUUID().toString());
                    }
                }
            });
            threadList.add(thread);
            thread.start();
        }

        for (Thread thread : threadList) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("size: " + list.size());
        ArrayHandleUtils.uniqueInList(list);
        System.out.println("unique size: " + list.size());

    }
}
